{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import roslaunch\n",
    "import rospy\n",
    "import os\n",
    "\n",
    "def add_launch(object_list,pkg,file,args=[]):\n",
    "    \"\"\"\n",
    "    查找launch路径\n",
    "    :param object_list: 启动列表，将在函数内修改\n",
    "    :param pkg: 功能包名称\n",
    "    :param file: launch文件名\n",
    "    :param arg: launch的可选参数配置列表,[\"param1 := num1\", \"param2:=num2\"] \n",
    "    :return: 添加是否成功\n",
    "    \"\"\"\n",
    "    ros_path = os.environ['ROS_PACKAGE_PATH']\n",
    "    cli_args = []\n",
    "    for dir in ros_path.split(':'):\n",
    "        path = os.path.join(dir,pkg)\n",
    "        if os.path.exists(path):\n",
    "            path = os.path.join(os.path.join(path,'launch'),file)\n",
    "            if os.path.exists:\n",
    "                cli_args.append(path)\n",
    "                if len(args) == 0:\n",
    "                    object_list.append(path)\n",
    "                else:\n",
    "                    for arg in args:\n",
    "                        cli_args.append(arg)\n",
    "                    object_list.append((roslaunch.rlutil.resolve_launch_arguments(cli_args)[0],cli_args[1:]))\n",
    "                return True\n",
    "        else:\n",
    "            rospy.logerr('未找到launch文件,pkg:'+pkg+',file:'+file)\n",
    "            return False\n",
    "    \n",
    "def create_launchList(launch):\n",
    "    launchList = []\n",
    "    for code in launch:\n",
    "        add_launch(launchList,code.split(' ')[1],code.split(' ')[2],code.split(' ')[3:])\n",
    "    uuid = roslaunch.rlutil.get_or_generate_uuid(str(os.getppid()), False)\n",
    "    rosbridge_launch = roslaunch.parent.ROSLaunchParent(uuid,launchList)\n",
    "    return rosbridge_launch     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# launch = create_launchList([{'pkg':'bobac3_navigation',\n",
    "#                              'file':'demo_nav_2d.launch',\n",
    "#                              'args':['open_nav_rviz:=false',\n",
    "#                                      'map_file_name:=0410-1']}])\n",
    "launch = create_launchList(['roslaunch bobac3_navigation demo_nav_2d.launch map_file_name:=0410-1 open_nav_rviz:=false'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "class test:\n",
    "    a = 1\n",
    "    b = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[0m[ INFO] [1732865681.984270274]: Finished loading Gazebo ROS API Plugin.\u001b[0m\n",
      "\u001b[0m[ INFO] [1732865681.985254631]: waitForService: Service [/gazebo_gui/set_physics_properties] has not been advertised, waiting...\u001b[0m\n",
      "\u001b[0m[ INFO] [1732861141.396630789]: Finished loading Gazebo ROS API Plugin.\u001b[0m\n",
      "\u001b[0m[ INFO] [1732861141.397383889]: waitForService: Service [/gazebo_gui/set_physics_properties] has not been advertised, waiting...\u001b[0m\n",
      "\u001b[1m[gazebo_gui-10] process has finished cleanly\n",
      "log file: /home/lyh/.ros/log/20061/gazebo_gui-10*.log\u001b[0m\n",
      "all processes on machine have died, roslaunch will exit\n",
      "\u001b[1m[gazebo_gui-3] process has finished cleanly\n",
      "log file: /home/lyh/.ros/log/20061/gazebo_gui-3*.log\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "test.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.split(' ')[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.split(' ')[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "launch.shutdown()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
